package com.goodving.guli.activity;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C1154e;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.eventbus.EventWeightTargetUpdated;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.WeightGoal;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.WeightGoalSetView;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.WeightGoalManager;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.BitmapDescriptorFactory;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/4.
 */
public class WeightGoalSetActivity extends SystemBarTintActivity implements View.OnClickListener, ag {
    public static final String f11178a = "UID";
    public static final String f11179b = "GOAL_WEIGHT";
    public static final int f11180c = 272;
    public static final int f11181d = 544;
    private static final String TAG = "WeightGoalSetActivity";
    float f11183e;
    private Context f11184g;
    private TextView f11185h;
    private WeightGoalSetView mWeightGoalSetView;
    private View f11187j;
    private View f11188k;
    private TextView f11189l;
    private int f11190m;
    private int f11191n;
    private int f11192o;
    private UserInfo mUserInfo;
    private int[] f11194q;
    private boolean f11195r;
    private int f11196s;

    public WeightGoalSetActivity() {
        this.f11184g = this;
        this.f11187j = null;
        this.f11188k = null;
        this.f11191n = 0;
        this.f11194q = new int[3];
        this.f11195r = false;
        this.f11183e = 0.0f;
    }

    private void m15638a() {
        this.f11185h = (TextView) findViewById(R.id.person_info_set_weight_info);
        this.mWeightGoalSetView = (WeightGoalSetView) findViewById(R.id.weight_goal_seekbar);
        int age = Birthday.fromStr(this.mUserInfo.birthday).getAge();
        this.mWeightGoalSetView.m15651b(0); // if the bar is fixed or not, 0 is not fixed.
        int i = this.f11191n;
        if (this.mUserInfo.height < 100) {
            if (age < 7) {
                this.f11185h.setText(getString(R.string.baby_weight_tips));
            } else {
                this.f11185h.setText(getString(R.string.weight_goal_height_100));
            }
            this.mWeightGoalSetView.setScaleRange(WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit), WeightUtil.m15706b(75.0f, Keeper.readPersonInfo().miliConfig.weightUnit));
            age = ((int) (WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit) + WeightUtil.m15706b(75.0f, Keeper.readPersonInfo().miliConfig.weightUnit))) / 2;
        } else if (age >= 7 && age <= 18) {
            float a = WeightUtil.m15692a(age, this.mUserInfo.gender);
            float b = WeightUtil.m15707b(age, this.mUserInfo.gender);
            a = WeightUtil.m15688a(a, ((float) this.mUserInfo.height) / 100.0f, Keeper.readPersonInfo().miliConfig.weightUnit);
            b = WeightUtil.m15688a(b, ((float) this.mUserInfo.height) / 100.0f, Keeper.readPersonInfo().miliConfig.weightUnit);
            this.f11185h.setText(getString(R.string.weight_less_than_tips, new Object[]{((int) (0.5f + a)) + WeightUtil.m15698a(this.f11184g, Keeper.readPersonInfo().miliConfig.weightUnit)}));
            this.mWeightGoalSetView.setScaleRange(WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit), a, b, WeightUtil.m15706b((float) BitmapDescriptorFactory.HUE_GREEN, Keeper.readPersonInfo().miliConfig.weightUnit));
            age = ((int) (WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit) + a)) / 2;
        } else if (age < 7) {
            this.f11185h.setText(getString(R.string.baby_weight_tips));
            this.mWeightGoalSetView.setScaleRange(WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit), WeightUtil.m15706b(75.0f, Keeper.readPersonInfo().miliConfig.weightUnit));
            age = ((int) (WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit) + WeightUtil.m15706b(75.0f, Keeper.readPersonInfo().miliConfig.weightUnit))) / 2;
        } else {
            this.f11185h.setText(getString(R.string.set_goal_weight_tips, new Object[]{Integer.valueOf((int) (((float) this.f11190m) + 0.5f)), Integer.valueOf((int) (((float) this.f11191n) + 0.5f)), WeightUtil.m15698a(this.f11184g, Keeper.readPersonInfo().miliConfig.weightUnit)}));
            this.mWeightGoalSetView.setScaleRange(WeightUtil.m15706b(3.0f, Keeper.readPersonInfo().miliConfig.weightUnit), (float) this.f11190m, (float) this.f11191n, (float) this.f11192o, WeightUtil.m15706b((float) BitmapDescriptorFactory.HUE_GREEN, Keeper.readPersonInfo().miliConfig.weightUnit));
            age = (this.f11190m + this.f11191n) / 2;
        }
        if (this.mUserInfo.targetWeight > 0.0f) {
            this.mWeightGoalSetView.m15647a(WeightUtil.m15716c(WeightUtil.m15706b(this.mUserInfo.targetWeight, Keeper.readPersonInfo().miliConfig.weightUnit), 1));
        } else if (WeightInfoManager.getInstance().m15758b(this.mUserInfo.uid) == 0) {
            this.mWeightGoalSetView.m15647a((float) age);
        } else {
            this.mWeightGoalSetView.m15647a(WeightUtil.m15716c(WeightUtil.m15706b(WeightInfoManager.getInstance().m15743a(this.mUserInfo.uid, 0).weight, Keeper.readPersonInfo().miliConfig.weightUnit), 1));
        }
        this.mWeightGoalSetView.m15649a((ag) this);
        this.f11188k = findViewById(R.id.right_button);
        this.f11188k.setOnClickListener(this);
        this.f11187j = findViewById(R.id.left_button);
        this.f11187j.setOnClickListener(this);
        if (this.f11195r) {
            ((TextView) this.f11187j).setText(getString(R.string.cancel));
            ((TextView) this.f11188k).setText(getString(R.string.set));
        } else {
            ((TextView) this.f11187j).setText(getString(R.string.clear_weight_goal));
            ((TextView) this.f11188k).setText(getString(R.string.reset_weight_goal));
        }
        this.f11189l = (TextView) findViewById(R.id.home_back);
        this.f11189l.setOnClickListener(this);
    }

    private void m15640b(float f) {
        if (this.f11196s == -1) {
            GuliLog.tracePrintInfo(TAG, "set goal succ");
            PersonInfo readPersonInfo = Keeper.readPersonInfo();
            readPersonInfo.targetWeight = WeightUtil.m15689a(f, Keeper.readPersonInfo().miliConfig.weightUnit);
            WeightGoalManager.getInstance().m15891a(new WeightGoal(this.f11184g, -1, readPersonInfo.weight, readPersonInfo.targetWeight, readPersonInfo.height));
            GuliLog.tracePrintInfo(TAG, "onActivityResult " + readPersonInfo);
            readPersonInfo.setNeedSyncServer(2);
            Keeper.keepPersonInfo(readPersonInfo);
            return;
        }
        this.mUserInfo.targetWeight = WeightUtil.m15689a(f, Keeper.readPersonInfo().miliConfig.weightUnit);
        WeightGoal weightGoal = new WeightGoal(this.f11184g, this.mUserInfo.uid, this.mUserInfo.weight, this.mUserInfo.targetWeight, this.mUserInfo.height);
        GuliLog.tracePrintInfo(TAG, "after save  the user is not the first time to set goal " + WeightGoalManager.getInstance().m15892a(this.mUserInfo.uid));
        WeightGoalManager.getInstance().m15891a(weightGoal);
        UserInfoManager.getInstance().m15599d(this.mUserInfo);
    }

    public void m15636a(float f) { //m15641a
        this.f11183e = WeightUtil.m15716c(f, 1);
    }

    static public class af extends DimPanelFragment {
        protected int inflateLayout() {
            return R.layout.dialog_weight_info_modify_delete_confirm;
        }

        public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
            View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
            Bundle arguments = getArguments();
            if (!(arguments == null || arguments.getString("Msg") == null)) {
                ((TextView) onCreateView.findViewById(R.id.msg)).setText(arguments.getString("Msg"));
            }
            return onCreateView;
        }
    }

    class ae implements C1154e {
        final /* synthetic */ WeightGoalSetActivity f11332a;

        ae(WeightGoalSetActivity weightGoalSetActivity) {
            this.f11332a = weightGoalSetActivity;
        }

        public void m10601b(DialogFragment dialogFragment) {  //m15725a
            dialogFragment.dismiss();
        }

        public void m10602c(DialogFragment dialogFragment) { //  m15726b
            dialogFragment.dismiss();
        }

        public void m10600a (DialogFragment dialogFragment) { //m15727c
            dialogFragment.dismiss();
            WeightGoalManager.getInstance().deleteUserWeightGoalInLocal(this.f11332a.f11196s);
            this.f11332a.mUserInfo.targetWeight = -1;
            UserInfoManager.getInstance().m15599d(this.f11332a.mUserInfo);
            Intent intent = new Intent();
            intent.putExtra(WeightGoalSetActivity.f11179b, -1);
            this.f11332a.setResult(WeightGoalSetActivity.f11181d, intent);
            this.f11332a.finish();
            EventBus.getDefault().post(new EventWeightTargetUpdated(this.f11332a.f11196s));
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_back:
                finish();
                break;
            case R.id.left_button:
                if (this.f11195r) {
                    finish();
                    return;
                }
                C0668a.m8126a(this.f11184g, C0668a.gj);
                String string = getString(R.string.no_need_weight_goal);
                af afVar = new af();
                afVar.setOpClickListener(new ae(this));
                Bundle bundle = new Bundle();
                bundle.putString("Msg", string);
                DimPanelFragment.showPanel((Activity) this, afVar, bundle);
                break;
            case R.id.right_button:
                m15640b(this.f11183e);
                setResult(f11180c, new Intent());
                finish();
                GuliLog.tracePrintInfo(TAG, "set value goal " + this.f11183e);
                EventBus.getDefault().post(new EventWeightTargetUpdated(this.f11196s));
                if (this.f11195r) {
                    C0668a.m8126a(this.f11184g, C0668a.gh);
                } else {
                    C0668a.m8126a(this.f11184g, C0668a.gi);
                }
                break;
            default:
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getIntent() != null) {
            GuliLog.tracePrintInfo(TAG, "getIntent is not null");
            this.f11196s = getIntent().getIntExtra(f11178a, -1);
            this.mUserInfo = UserInfoManager.getInstance().getInfoByUid(this.f11196s);
            GuliLog.tracePrintInfo(TAG, "mUserInfo is " + this.mUserInfo);
            int i = 0;
            for (String str : WeightUtil.m15719d(((float) this.mUserInfo.height) / 100.0f, Birthday.fromStr(this.mUserInfo.birthday).getAge(), Keeper.readPersonInfo().miliConfig.weightUnit).split(",")) {
                GuliLog.tracePrintInfo(TAG, "scale is " + str);
                this.f11194q[i] = (int) Float.parseFloat(str);
                i++;
            }
            this.f11190m = this.f11194q[0];
            this.f11191n = this.f11194q[1];
            this.f11192o = this.f11194q[2];
        }
        if (this.mUserInfo.targetWeight > 0.0f) {
            this.f11195r = false;
        } else {
            this.f11195r = true;
        }
        setContentView((int) R.layout.activity_person_info_set_weightgoal);
        m15638a();
    }

    protected void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.aV);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aV);
        C0668a.m8124a((Activity) this);
    }
}
